package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> pageQueryDevice(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));
        Page<DeviceDataVo> pageResponse = deviceDataMapper.pageQueryDevice(deviceDataPageReqDto);
        return PageResponse.of(pageResponse, DeviceDataVo.class);
    }
}
